<?php
// +----------------------------------------------------------------------
// |     【FabricMain】Controller 控制器
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\admin\controller\fabric;

use think\facade\Request;
use app\common\service\FabricMain as S;
use app\common\model\FabricMain as M;
use app\common\model\SupplierMain as SUP;

class Main extends  \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck','AdminPermission'];

	//初始化
	public function initialize(){
		parent::initialize();
		
		$this->assign('controller_title','面料管理');
	}

    // 列表
    public function index(){
        if (Request::isAjax()) {
            return $this->getJson(M::getList());
        }
        return $this->fetch();
    }

    // 添加
    public function add(){
        if (Request::isAjax()) {
            return $this->getJson(S::goAdd(Request::post()));
        }
        return $this->fetch('edit',[
			'action_up'=>false,
            'supplier'=>SUP::listData(),
            'options'=>config_cache_file('options'),
		]);
    }

    // 编辑
    public function edit($id){
        if (Request::isAjax()) {
            return $this->getJson(S::goEdit(Request::post(),$id));
        }
		
		$model=M::info(['id'=>$id]);
		if(!$model) $this->getJson(102);
        
		return $this->fetch('',[
			'action_up'=>true,
			'model' => $model,
            'supplier'=>SUP::listData(),
            'options'=>config_cache_file('options'),
		]);
    }
    
    // 复制产品
    public function copy(){
    	if (Request::isAjax()) {
    		$id=I('post.ids','');
    		$id=explode('|',$id);
    		return $this->getJson(S::copy($id));
    	}else{
    		return $this->getJson(404);
    	}
    }

    // 状态
    public function status($id){
        return $this->getJson(S::goStatus(Request::post(),$id));
    }

    // 删除
    public function remove($id){
        return $this->getJson(S::goRemove($id));
    }

    // 批量删除
    public function batchRemove(){
        return $this->getJson(S::goBatchRemove([
				['id','in',Request::post('ids')],
			]));
    }

    // 回收站
    public function recycle(){
        if (Request::isAjax()) {
            return $this->getJson(S::goRecycle());
        }
        return $this->fetch();
    }
	
    // 设置某个值
    public function setValue(){
        if (Request::isAjax()) {
            return $this->getJson(S::setFieldValue(Request::post()));
        }
        return $this->fetch();
    }
}
